﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class NhaXuatBanDAO
    {
        #region 1. Them
        public static bool ThemNXB(NhaXuatBanDTO nxb)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("MaNXB", nxb.MaNXB));
                sqlParams.Add(new SqlParameter("TenNXB", nxb.TenNXB));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertNHAXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Sua
        public static bool SuaNXB(NhaXuatBanDTO nxb)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("MaNXB", nxb.MaNXB));
                sqlParams.Add(new SqlParameter("TenNXB", nxb.TenNXB));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateNHAXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaNXB(String manxb){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("MaNXB", manxb));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteNHAXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static NhaXuatBanDTO TimNXB(String manxb){
            NhaXuatBanDTO result = new NhaXuatBanDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaNXB", manxb));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectNHAXB", sqlParams);

                DataRow dr = dt.Rows[0];
                result.MaNXB = dr["MaNXB"].ToString();
                result.TenNXB = dr["TenNXB"].ToString();

            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaNXB(){
           DataTable dt= new DataTable();
            try
            {
                 dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectNHAXBsAll");
               
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static bool CheckNXBByID(String manxb){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaNXB", manxb));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectNHAXB", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
    }
}
